﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for phrases data manipulation.
    /// </summary>
    public class PhrasesRepository : BaseRepository<Phrase>, IPhrasesRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PhrasesRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public PhrasesRepository(IDataServiceContext<Phrase> dataServiceContext)
            :base(dataServiceContext)
        {

        }

        /// <summary>
        /// Gets the phrases by user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public List<Phrase> GetPhrasesByUser(string userID)
        {
            var phrases = (from phrase in Entities
                           where phrase.User == userID
                           select phrase).OrderByDescending(p => p.Date).ToList();

            return phrases;
        }

        /// <summary>
        /// Deletes all user phrases.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllUserPhrases(string userID)
        {
            var phrases = GetPhrasesByUser(userID);

            phrases.ForEach(p => Delete(p));
        }
    }
}
